﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using Bots.Professionbuddy.PropertyGridUtilities.Editors;
using DesmoPlugin;

namespace DesmoPlugin
{
    public partial class AuctionPrice : Form
    {

        public AuctionPrice()
        {
            InitializeComponent();
        }
        public string currentSetting;
        /*
        string Gold;
        string Silver;
        string Copper;
         */
        XmlDocument doc = new XmlDocument();

        //public static MyListView auctGrid = new MyListView();
        //XmlHandler xmlSettings = new XmlHandler();

        public void AuctionSettings(string sender)
        {
            this.Text = "Auction Settings - " + sender + " -";
            currentSetting = sender;
            for (int i = 0; i < 350; i++)
            {
                if (sender == XmlHandler.XmlSettings[3, i])
                {
                    GoldEditor Min = new GoldEditor(XmlHandler.XmlSettings[5, i]);
                    valueAuctMinGold.Text = Min.Gold.ToString();
                    valueAuctMinSilver.Text = Min.Silver.ToString();
                    valueAuctMinCopper.Text = Min.Copper.ToString();
                    GoldEditor Max = new GoldEditor(XmlHandler.XmlSettings[6, i]);
                    valueAuctMaxGold.Text = Max.Gold.ToString();
                    valueAuctMaxSilver.Text = Max.Silver.ToString();
                    valueAuctMaxCopper.Text = Max.Copper.ToString();
                    GoldEditor Fall = new GoldEditor(XmlHandler.XmlSettings[7, i]);
                    valueAuctFallGold.Text = Fall.Gold.ToString();
                    valueAuctFallSilver.Text = Fall.Silver.ToString();
                    valueAuctFallCopper.Text = Fall.Copper.ToString();
                    valueAuctStack.Text = XmlHandler.XmlSettings[9, i];
                    valueAuctStackSize.Text = XmlHandler.XmlSettings[8, i];
                    switch (XmlHandler.XmlSettings[4, i])
                    {
                        case "_12_Hours":
                            menuAuctDuration.SelectedIndex = 0;
                            break;

                        case "_24_Hours":
                            menuAuctDuration.SelectedIndex = 1;
                            break;

                        case "_48_Hours":
                            menuAuctDuration.SelectedIndex = 2;
                            break;

                        default:
                            menuAuctDuration.SelectedIndex = 0;
                            break;
                    }
                    

                    break;
                }
            }
            ShowDialog();

        }

        private void Cancel_Price_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Default_Price_Click(object sender, EventArgs e)    
        {
            for (int i = 0; i < 350; i++)
            {
                if (XmlHandler.XmlSettings[3, i] == "Default Auction")
                {

                    valueAuctStack.Text = XmlHandler.XmlSettings[9, i];
                    valueAuctStackSize.Text = XmlHandler.XmlSettings[8, i];
                    switch (XmlHandler.XmlSettings[4, i])
                    {
                        case "_12_Hours":
                            menuAuctDuration.SelectedIndex = 0;
                            break;

                        case "_24_Hours":
                            menuAuctDuration.SelectedIndex = 1;
                            break;

                        case "_48_Hours":
                            menuAuctDuration.SelectedIndex = 2;
                            break;

                        default:
                            menuAuctDuration.SelectedIndex = 0;
                            break;
                    }

                    GoldEditor Max = new GoldEditor(XmlHandler.XmlSettings[6, i]);
                    valueAuctMaxGold.Text = Max.Gold.ToString();
                    valueAuctMaxSilver.Text = Max.Silver.ToString();
                    valueAuctMaxCopper.Text = Max.Copper.ToString();

                    GoldEditor Min = new GoldEditor(XmlHandler.XmlSettings[5, i]);
                    valueAuctMinGold.Text = Min.Gold.ToString();
                    valueAuctMinSilver.Text = Min.Silver.ToString();
                    valueAuctMinCopper.Text = Min.Copper.ToString();

                    GoldEditor Fall = new GoldEditor(XmlHandler.XmlSettings[7, i]);
                    valueAuctFallGold.Text = Fall.Gold.ToString();
                    valueAuctFallSilver.Text = Fall.Silver.ToString();
                    valueAuctFallCopper.Text = Fall.Copper.ToString();
                    
                }
            }
        }

        private void Copy_Price_Click(object sender, EventArgs e)
        {
            
            DialogResult copyCheck = MessageBox.Show("Are you sure to copy these values to other crafts?", "Copy Settings?", MessageBoxButtons.YesNo);
            switch (copyCheck)
            {
                case DialogResult.Yes:
                    {

                        //valueAuctMinGold.Text + "g" + valueAuctMinSilver.Text + "s" + valueAuctMinCopper.Text + "c"

                        foreach (Control c in this.Controls)
                        {
                            TextBox text = c as TextBox;
                            if (text != null)
                            {
                                //ReadXml(text);

                            }
                            

                        }
                        break;
                    }
                    
                case DialogResult.No: 
                    {
                        
                        break;
                    }
            }
        }

        

        private void Save_Price_Click(object sender, EventArgs e)
        {
            
            doc.Load(XmlHandler.path);
            for (int i = 0; i < 350; i++)
            {
                if (XmlHandler.XmlSettings[3, i] != null)
                {
                    if (currentSetting == XmlHandler.XmlSettings[3, i])
                    {
                        switch (menuAuctDuration.SelectedIndex)
                        {
                            case 0:
                                XmlHandler.XmlSettings[4, i] = "_12_Hours";
                                break;

                            case 1:
                                XmlHandler.XmlSettings[4, i] = "_24_Hours";
                                break;

                            case 2:
                                XmlHandler.XmlSettings[4, i] = "_48_Hours";
                                break;

                            default:
                                XmlHandler.XmlSettings[4, i] = "_12_Hours";
                                break;
                        }
                        XmlHandler.XmlSettings[5, i] = valueAuctMinGold.Text + "g" + valueAuctMinSilver.Text + "s" + valueAuctMinCopper.Text + "c";
                        XmlHandler.XmlSettings[6, i] = valueAuctMaxGold.Text + "g" + valueAuctMaxSilver.Text.ToString() + "s" + valueAuctMaxCopper.Text + "c";
                        XmlHandler.XmlSettings[7, i] = valueAuctFallGold.Text + "g" + valueAuctFallSilver.Text + "s" + valueAuctFallCopper.Text + "c";
                        XmlHandler.XmlSettings[8, i] = valueAuctStackSize.Text;
                        XmlHandler.XmlSettings[9, i] = valueAuctStack.Text;

                        IEnumerator ie = doc.SelectNodes("/Professionbuddy/SubRoutine/Settings").GetEnumerator();

                        ie.Reset();
                        while (ie.MoveNext())
                        {
                            if ((ie.Current as XmlNode).Attributes["Name"].Value == XmlHandler.XmlSettings[0, i])
                            {
                                (ie.Current as XmlNode).Attributes["RunTime"].Value = XmlHandler.XmlSettings[4, i];
                                (ie.Current as XmlNode).Attributes["MinBuyout"].Value = XmlHandler.XmlSettings[5, i];
                                (ie.Current as XmlNode).Attributes["MaxBuyout"].Value = XmlHandler.XmlSettings[6, i];
                                (ie.Current as XmlNode).Attributes["FallBackPrice"].Value = XmlHandler.XmlSettings[7, i];
                                (ie.Current as XmlNode).Attributes["StackSize"].Value = XmlHandler.XmlSettings[8, i];
                                (ie.Current as XmlNode).Attributes["Amount"].Value = XmlHandler.XmlSettings[9, i];

                                break;
                            }

                        }
                        

                    }
                }
                
             }
            doc.Save(XmlHandler.path);
            //auctGrid.GridRefresh();
            //MyListView.GridRefresh();
            //DesmoPluginCFG.auctGrid.GridRefresh();
            this.Close();
        }

        

    }
}
